package com.reebake.ideal.message.handler;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.reebake.ideal.message.entity.MessageDeliveryFeedbackDTO;
import com.reebake.ideal.message.entity.MessageHistory;
import com.reebake.ideal.message.event.MessageDeliveryFeedbackEvent;
import com.reebake.ideal.message.mapper.MessageHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MessageDeliveryFeedbackListener {
    @Autowired
    private MessageHistoryMapper messageHistoryMapper;

    @EventListener
    void handle(MessageDeliveryFeedbackEvent messageDeliveryFeedbackEvent){
        MessageDeliveryFeedbackDTO messageDeliveryFeedbackDTO = messageDeliveryFeedbackEvent.getMessageDeliveryFeedbackDTO();
        String messageHistoryId = messageDeliveryFeedbackDTO.getMessageHistoryId();
        if(StrUtil.isBlank(messageHistoryId)){
            return;
        }
        LambdaUpdateWrapper<MessageHistory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(MessageHistory::getDeliveryStatus, messageDeliveryFeedbackDTO.getDeliveryStatus())
                .set(MessageHistory::getRemark, messageDeliveryFeedbackDTO.getReason())
                .set(MessageHistory::getDeliveryTime, messageDeliveryFeedbackDTO.getDeliveryTime())
                .eq(MessageHistory::getMessageHistoryId, messageHistoryId);
        messageHistoryMapper.update(updateWrapper);
    }
}
